#include "debug-helper.h"
#include "line-edit-delegate.h"
#include "value-result.h"
#include "worker-section-model.h"
#include "worker-section-queries.h"

WorkerSectionModel::WorkerSectionModel(QObject *parent) :
    StandardDatabaseModel(parent)
{
}

void WorkerSectionModel::initialize()
{
    m_queries = new WorkerSectionQueries(this);
    LineEditDelegate* nameDelegate = new LineEditDelegate(this);
    LineEditDelegate* surnameDelegate = new LineEditDelegate(this);
    m_delegates.insert(NameColumn, nameDelegate);
    m_delegates.insert(SurnameColumn, surnameDelegate);
    m_headerSections << "Imię" << "Nazwisko";
    m_dataAlreadyExistText = "Pracownik o takich danych już istnieje.";
    StandardDatabaseModel::initialize();
}

ValueResult* WorkerSectionModel::canSetField (QVariant value, int column, int row)
{
    Q_UNUSED(row);
    ValueResult* result = new ValueResult(this);
    if (column == NameColumn)
    {
        QString name(value.toString());
        if (name.isEmpty())
            result->setError("Imię pracownika jest puste.", SmGlobal::DataInvalidError);
        else
            result->setValue(true);
    }
    else if (column == SurnameColumn)
    {
        QString surname(value.toString());
        if (surname.isEmpty())
            result->setError("Nazwisko pracownika jest puste.", SmGlobal::DataInvalidError);
        else
            result->setValue(true);
    }
    else
    {
        fatalError(column);
        return 0;
    }
    return result;
}
